import request from '@/utils/request'
import {formatDateTime} from '@/utils/dateUtil'

const api = {}
let userStateMap = {1:'正常',2:'注销',3:'删除',4:'锁定'}

//用户状态下拉框
api.getUserStateList = () => {
	let userStateList = [];
	for(var k in userStateMap) {
		userStateList.push({value:parseInt(k), label:userStateMap[k]})
	}
	return userStateList
}

//系统用户集合查询
api.getUserList = data => {
  let res = request({
    url: `/base/sysUser/query/${data.pageNo}/${data.pageSize}`,
    method: 'post',
    data
  })
  res = res.then( res => {
  	let page = res.data.page;
    page.list.forEach(item => {
    	//用户状态处理
    	if(item.userState==1){
    		item.userStateStr = userStateMap[item.userState]
    	} else {
			item.userStateStr = '<span style="color:#f00">'+userStateMap[item.userState]+'</span>'
    	}
    	
    	item.createTime = formatDateTime(item.createTime)
    })
    return page
  })
  return res
}

//查询单个用户信息
api.get = id => {
	return request({
    	url: `/base/sysUser/get/${id}`,
    	method: 'get'
  	})
}

//更新
api.update = data => {
	return request({
    	url: `/base/sysUser/update`,
    	method: 'post',
    	data
  	})
}

//重置密码
api.resetPwd = userId => {
	return request({
    	url: `/base/sysUser/resetPwd/${userId}`,
    	method: 'post'
  	})
}

//新增用户
api.save = data => {
	return request({
    	url: `/base/sysUser/save`,
		method: 'post',
		data
  	})
}

//用户角色查询接口
api.getSysUserRoleList = data => {
	return request({
    	url: `/base/itouchSysUserRole/getSysUserRoleList`,
		method: 'get',
		params: data
  	})
}


//用户角色添加
api.addSysUserRole = data => {
	return request({
    	url: `/base/itouchSysUserRole/addSysUserRole?roleIds=${data.roleIds}&userId=${data.userId}`,
		method: 'post'
  	})
}

export default api
